﻿using UnityEngine;

namespace JH
{
    public static class RectContainsExtension
    {
        /// <summary>
        ///  判断一个矩形是否包含另一个矩形
        /// </summary>
        ///  <param name="rectA">矩形A</param>
        ///  <param name="rectB">矩形B</param>
        /// <param name="cornerContains">四个角落是否被包含</param>
        public static bool ContainsEm(this Rect rectA, Rect rectB, out CornerBool cornerContains)
        {
            // 检查 rectB 的四个角是否都在 rectA 内
            bool bottomLeftContained = rectA.Contains(rectB.min);
            bool topLeftContained = rectA.Contains(new Vector2(rectB.xMin, rectB.yMax));
            bool topRightContained = rectA.Contains(rectB.max);
            bool bottomRightContained = rectA.Contains(new Vector2(rectB.xMax, rectB.yMin));

            cornerContains = new CornerBool
            {
                LeftTop = topLeftContained,
                LeftBottom = bottomLeftContained,
                RightTop = topRightContained,
                RightBottom = bottomRightContained
            };

            // 如果四个角都在 rectA 内，那么 rectA 包含 rectB
            return bottomLeftContained && topLeftContained && topRightContained && bottomRightContained;
        }

        /// <summary>
        ///  是否存在重叠
        /// </summary>
        public static bool OverLapEm(this Rect rectA, Rect rectB)
        {
            ContainsEm(rectA, rectB, out var cornerContains);
            return rectA.xMin < rectB.xMax && rectA.xMax > rectB.xMin &&
                   rectA.yMin < rectB.yMax && rectA.yMax > rectB.yMin;
        }
    }

    /// <summary>
    ///   四个角的布尔值
    /// </summary>
    public struct CornerBool
    {
        public bool LeftTop { get; set; }
        public bool LeftBottom { get; set; }
        public bool RightTop { get; set; }
        public bool RightBottom { get; set; }
    }
}